package com.shawn.mobliesafe;

import android.content.SharedPreferences;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class DragViewActivity extends AppCompatActivity {

    private LinearLayout ll_dragview_toast;
    private SharedPreferences sp;
    private int width;
    private int height;
    private TextView tv_dragview_bottom;
    private TextView tv_dragview_top;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drag_view);
        sp = getSharedPreferences("config",MODE_PRIVATE);
        ll_dragview_toast = (LinearLayout)findViewById(R.id.ll_dragview_toast);
        tv_dragview_bottom = (TextView)findViewById(R.id.tv_dragview_bottom);
        tv_dragview_top = (TextView)findViewById(R.id.tv_dragview_top);

        WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(metrics);
        width = metrics.widthPixels;
        height = metrics.heightPixels;

        int x = sp.getInt("x",0);
        int y = sp.getInt("y",0);
        //在初始化控件之前重新设置控件的属性
        //获取父控件的属性规则,父控件的layoutparams
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ll_dragview_toast.getLayoutParams();
        //设置相应的属性
        //leftMargin : 距离父控件左边的距离,与布局文件中控件中layout_marginLeft属性效果相似
        params.leftMargin = x;
        params.topMargin = y;
        //给控件设置属性
        ll_dragview_toast.setLayoutParams(params);


        if (y >= height/2) {
            //隐藏下方显示上方
            tv_dragview_bottom.setVisibility(View.INVISIBLE);
            tv_dragview_top.setVisibility(View.VISIBLE);
        }else{
            //隐藏上方显示下方
            tv_dragview_top.setVisibility(View.INVISIBLE);
            tv_dragview_bottom.setVisibility(View.VISIBLE);
        }

        setTouch();
        setDoubleClick();
    }

    /**
     * 设置触摸监听
     */
    private void setTouch() {
        ll_dragview_toast.setOnTouchListener(new View.OnTouchListener() {
            private int startX;
            private int startY;
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {//event.getAction() : 获取控制的执行的事件
                    case  MotionEvent.ACTION_DOWN://按下的事件
                        //1.按下控件,记录开始的x和y的坐标
                        startX = (int) event.getRawX();
                        startY = (int) event.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        //2.移动到新的位置记录新的位置的x和y的坐标
                        int newX = (int) event.getRawX();
                        int newY = (int) event.getRawY();
                        //3.计算移动的偏移量
                        int dX = newX- startX;
                        int dY = newY-startY;
                        //4.移动相应的偏移量,重新绘制控件
                        //获取的时候原控件距离父控件左边和顶部的距离
                        int l = ll_dragview_toast.getLeft();
                        int t = ll_dragview_toast.getTop();
                        //获取新的控件的距离父控件左边和顶部的距离
                        l += dX;
                        t += dY;
                        int r = l + ll_dragview_toast.getWidth();
                        int b = t + ll_dragview_toast.getHeight();
                        if(l < 0 || r >width || t < 0 || b > height - 25) {
                            break;
                        }
                        ll_dragview_toast.layout(l,t,r,b);//重新绘制控件

                        //判断textview的隐藏显示
                        int top = ll_dragview_toast.getTop();
                        if (top >= height/2) {
                            //隐藏下方显示上方
                            tv_dragview_bottom.setVisibility(View.INVISIBLE);
                            tv_dragview_top.setVisibility(View.VISIBLE);
                        }else{
                            //隐藏上方显示下方
                            tv_dragview_top.setVisibility(View.INVISIBLE);
                            tv_dragview_bottom.setVisibility(View.VISIBLE);
                        }
                        //5.更新开始的坐标
                        startX=newX;
                        startY=newY;
                        break;
                    case MotionEvent.ACTION_UP:
                        int x = ll_dragview_toast.getLeft();
                        int y = ll_dragview_toast.getTop();
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putInt("x",x);
                        editor.putInt("y",y);
                        editor.commit();
                        break;
                }
                
                return false;
            }
        });
    }

    long [] mHits = new long[2];
    /**
     * 双击居中
     */
    private void setDoubleClick(){
        ll_dragview_toast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**
                 *  src the source array to copy the content.   拷贝的原数组
                 srcPos the starting index of the content in src.  是从源数组那个位置开始拷贝
                 dst the destination array to copy the data into.  拷贝的目标数组
                 dstPos the starting index for the copied content in dst.  是从目标数组那个位置开始去写
                 length the number of elements to be copied.   拷贝的长度
                 */
                //拷贝数组操作

                System.arraycopy(mHits,1,mHits,0,mHits.length-1);
                mHits[mHits.length-1] = SystemClock.uptimeMillis();// 将离开机的时间设置给数组的第二个元素,离开机时间 :毫秒值,手机休眠不算
                if(mHits[0] > (SystemClock.uptimeMillis() - 500)) {
                    int l = (width - ll_dragview_toast.getWidth())/2;
                    int t = (height - 25 - ll_dragview_toast.getHeight())/2;
                    ll_dragview_toast.layout(l, t, l + ll_dragview_toast.getWidth(), t + ll_dragview_toast.getHeight());
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putInt("x",l);
                    editor.putInt("y",t);
                    editor.commit();
                }
            }
        });
    }

}
